<?php
class Strings {
	/**
	 * 
	 * @param unknown_type $string
	 * @return string
	 */
	public function stringToHex($string) {
		$hex = '';
		for($i = 0; $i < strlen ( $string ); $i ++) {
			$hex .= dechex ( ord ( $string [$i] ) );
		}
		return $hex;
	}
	/**
	 * 
	 * @param unknown_type $hex
	 * @return string
	 */
	public function hexToString($hex) {
		$string = '';
		for($i = 0; $i < strlen ( $hex ) - 1; $i += 2) {
			$string .= chr ( hexdec ( $hex [$i] . $hex [$i + 1] ) );
		}
		return $string;
	}
	
	public function stringFromColumnIndex($colNumber) {//(Example 1 = A, 2 = B)
		$numeric = $colNumber % 26;
		$letter = chr(65 + $numeric);
		$num2 = intval($colNumber / 26);
		if ($num2 > 0) {
			return self::stringFromColumnIndex($num2 - 1) . $letter;
		} else {
			return $letter;
		}
	}
	
	public function columnIndexFromString($colString){//(Example A = 0, B = 1)
		$base = 64;
		$decimal = 26;
		$columnIndex = 0;
		for ($i = 0; $i < strlen($colString); $i++) {
			$char = ord($colString[$i]);
			$char -= $base;
			$char *= pow($decimal, (strlen($colString) - ($i + 1)));
			$columnIndex += $char;
		}
		return $columnIndex;
	}
	
	function email($email){                  
		return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);          
	} 
	
	function randrom($len) {                  
		$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
		'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6',
		'7', '8', '9');                  
		$e = count($chars) - 1;                  
		shuffle($chars);                  
		$output = '';                  
		for ($i=0; $i< $len; $i++) {                          
			$output .= $chars[mt_rand(0, $e)];                  
		}                  
		return $output;          
	}  
	
	public static function token(){
		$token = md5(uniqid(rand(), true));
		return $token;
	}
}